 <template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon
          name="cross"
          size="0.37333334rem"
          color="white"
          @click="closePopupFn"
          />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道
          点击
          <span class="small-title">
            {{ isShowCross ? '删除' : '进入' }}
          </span>
          频道
        </span>
        <span @click="isShowCross = !isShowCross">{{ isShowCross ? '完成' : '编辑' }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6"
            v-for="(obj,idx) in userChannelList"
            :key="obj.id"
            @click="$emit('enter-channel', obj.id)"
          >
          <div class="channel-item van-hairline--surround">
            {{ obj.name }}
            <!-- 删除的徽标 -->
            <van-badge color="transparent" class="cross-badge">
              <template #content>
                <van-icon
                  v-if="isShowCross && obj.id !== 0"
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                  @click="delChannelFn(idx)"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6"
          v-for="obj in unSelectedChannelList"
          :key="obj.id"
          @click="addChannelFn(obj)"
          >
          <div class="channel-item van-hairline--surround">{{ obj.name }}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { getAllChannelListAPI } from '@/api/home'
import { Toast } from 'vant'
export default {
  name: 'ChannelEdit',
  props: {
    // 父传子的用户已选择的频道列表
    userChannelList: Array
  },
  data () {
    return {
      allChannelList: [], // 所有的频道列表
      isShowCross: false // 是否显示 x 按钮，false表示不显示
    }
  },
  async created () {
    const resp = await getAllChannelListAPI()
    this.allChannelList = resp.data.channels
  },
  computed: {
    // 根据所有的频道列表 和 已选择频道列表，筛选出未选择的
    unSelectedChannelList () {
      return this.allChannelList.filter(obj => {
        // 判断obj 的id在不在userChannelList中，如果在，干掉；否则留下
        // some 跟 every 相对的
        // every: 检测数组的所有的元素都要满足某个条件，如果都满足，则返回true；否则返回false
        // some: 检测数组中是否有满足条件的元素，如果有，则返回true；否则返回false
        const flag = this.userChannelList.some(item => item.id === obj.id)
        if (flag === true) {
          // 如果在，不需要留在
          return false
        } else {
          // 否则留下
          return true
        }
      })
    }
  },
  methods: {
    closePopupFn () {
      // 1. 子传父
      this.$emit('close-popup')
      // 2. 重置isShowCross为false状态
      this.isShowCross = false
    },
    addChannelFn (item) {
      if (!this.isShowCross) {
        Toast('亲，编辑状态下方可添加！')
        return
      }
      this.$emit('add-channel', item)
    },
    delChannelFn (idx) {
      // 这里判断是没有必要，因为不点击编辑按钮，X小图标不会渲染
      if (this.isShowCross) {
        // 子传父
        this.$emit('del-channel', idx)
      }
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
